<template>
  <div style="margin-left: 5%; margin-right: 5%; margin-top: 5%">
    <el-button
      type="primary"
      style="margin-bottom: 10px"
      @click="dialogFormVisible = true"
      >添加</el-button
    >
    <el-table :data="filterTableData" border style="width: 100%">
      <el-table-column label="id" prop="userId" />
      <el-table-column label="账号" prop="userName" />
      <el-table-column label="角色" prop="role">
        <template #default="scope">
          {{ LevelEnum(scope.row.role) }}
        </template>
      </el-table-column>
      <el-table-column align="right">
        <template #header>
          <el-input
            v-model="search"
            size="small"
            placeholder="Type to search"
          />
        </template>
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button size="small" @click="handleReset(scope.$index, scope.row)"
            >重置密码</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogFormVisible" title="添加">
      <el-form :model="form">
        <el-form-item label="用户名" required :label-width="formLabelWidth">
          <el-input v-model="form.userName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色" required :label-width="formLabelWidth">
          <el-select v-model="form.role" placeholder="选择角色">
            <el-option v-if="store.userLevel < 2" label="管理员" value="2" />
            <el-option label="运营人员" value="3" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="addUser"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, ref, onMounted, reactive } from "vue";
import {
  apiGetUserList,
  apiDeleteUser,
  apiResetUserPwd,
  apiCreateUser,
} from "../../apis/userApi";
import { useStore } from "../../stores/system";
import { ElMessage } from "element-plus";

const store = useStore();
const tableData = ref([]);
const search = ref("");
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const form = reactive({
  userName: "",
  role: "",
});
const LevelEnum = (num) => {
  switch (num) {
    case 1:
      return "超级管理员";
    case 2:
      return "管理员";
    case 3:
      return "运营人员";
  }
};
const filterTableData = computed(() =>
  tableData.value.filter(
    (data) =>
      !search.value ||
      JSON.stringify(data).toLowerCase().includes(search.value.toLowerCase())
  )
);
onMounted(() => {
  getList();
});
const getList = () => {
  apiGetUserList({ role: store.userLevel }).then((res) => {
    console.log(res);
    tableData.value = res;
  });
};
const handleEdit = (index, row) => {
  console.log(index, row);
};
const handleReset = (index, row) => {
  apiResetUserPwd({ userId: row.userId }).then((res) => {
    if (res.code > 0) {
      ElMessage({
        message: "重置成功!",
        type: "success",
      });
    } else {
      ElMessage({
        message: "重置失败!" + res.msg,
        type: "error",
      });
    }
  });
};
const handleDelete = (index, row) => {
  apiDeleteUser({ userId: row.userId }).then((res) => {
    if (res.code > 0) {
      ElMessage({
        message: "删除成功!",
        type: "success",
      });
      getList();
    } else {
      ElMessage({
        message: "删除失败!" + res.msg,
        type: "error",
      });
    }
  });
};
const addUser = () => {
  if (form.role && form.userName) {
    apiCreateUser({ ...form, fatherId: sessionStorage.getItem("userId") }).then(
      (res) => {
        if (res.code > 0) {
          dialogFormVisible.value = false;
          ElMessage({
            message: "添加成功!",
            type: "success",
          });
          getList();
        } else {
          ElMessage({
            message: "添加失败!" + res.msg,
            type: "error",
          });
        }
      }
    );
  } else {
    ElMessage({
      message: "请输入账号或角色",
      type: "error",
    });
  }
};
</script>
<style scoped>
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
